package com.controller;



import com.entity.Document;
import com.entity.StoreHouse;
import com.entity.User;
import com.entity.base.Result;
import com.service.DocumentService;
import com.service.StoreHouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
@RequestMapping(value = "/admin")
@Slf4j
@Api(tags = "管理员相关接口", description = "提供相关的 Rest API")
public class AdminController {

    @Autowired
    StoreHouseService storeHouseService;

    @Autowired
    DocumentService documentService;

    @ApiOperation("创建资源库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "管理员", required = true, dataType = "user", paramType = "body")
    })
    @PostMapping("/createStoreHouse")
    public Result createStoreHouse(@RequestBody User user){
        if (user.getRoleId()!=3) return Result.error("无权限");
        StoreHouse storeHouse = new StoreHouse();
        storeHouse.setStatus(1);
        storeHouse.setTCapacity(100.0*1024);//100M存储空间
        storeHouse.setUCapacity(0.0);
        storeHouse.setRCapacity(100.0*1024);
        storeHouse.setRoleId(2);
        String fileName = "admin"+String.valueOf(System.currentTimeMillis());
        String filePath = System.getProperty("user.dir")+System.getProperty("file.separator")+"Files";
        //如果文件路径不存在，新增该路径
        File file1 = new File(filePath);
        if(!file1.exists()){
            file1.mkdir();
        }
        //实际的文件地址
        File dest = new File(filePath+System.getProperty("file.separator")+fileName);
        //存储到数据库里的相对文件地址
        String storeAvatorPath = "/Files/"+fileName;
        if(!dest.exists()){
            dest.mkdir();
            storeHouse.setPath(storeAvatorPath);
            return Result.success(storeHouse);
        }
            return Result.error("创建失败");
    }
    @ApiOperation("删除资源库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeHouse", value = "资源库", required = true, dataType = "user", paramType = "body")
    })
    @PostMapping("/deleteStoreHouse")
    public Result deleteStoreHouse(@RequestBody StoreHouse storeHouse){
        String path = System.getProperty("user.dir")+storeHouse.getPath();
        if (documentService.deleteFolder(path)) {
            storeHouseService.removeById(storeHouse.getId());
            return Result.success();
        }
        return Result.error("删除失败");
    }
    @ApiOperation("上传文件")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "avatorFile", value = "文件", required = true, dataType = "avatorFile", paramType = "body"),
            @ApiImplicitParam(name = "document", value = "文件信息", required = true, dataType = "avatorFile", paramType = "body")
    })
    @PostMapping("/uploadFile")
    public Result uploadFile(@RequestParam("file") MultipartFile avatorFile, @RequestParam("document")Document document){
        return documentService.uploadFile(avatorFile,document);
    }
}
